package linechart;

import common.ChartDemo;
import common.ChartUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.util.ShapeUtils;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;

import java.awt.*;

public class LineChartDemo8 extends ChartDemo {

    public LineChartDemo8(String title) {
        super(title);
    }

    @Override
    protected Dataset createDataset() {
        DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
        defaultCategoryDataset.addValue(0.0D, "Series 1", "Category 1");
        defaultCategoryDataset.addValue(2D, "Series 1", "Category 2");
        defaultCategoryDataset.addValue(1.0D, "Series 1", "Category 3");
        defaultCategoryDataset.addValue(3D, "Series 1", "Category 4");
        defaultCategoryDataset.addValue(5D, "Series 1", "Category 5");
        defaultCategoryDataset.addValue(2D, "Series 2", "Category 1");
        defaultCategoryDataset.addValue(4D, "Series 2", "Category 2");
        defaultCategoryDataset.addValue(4D, "Series 2", "Category 3");
        defaultCategoryDataset.addValue(5D, "Series 2", "Category 4");
        defaultCategoryDataset.addValue(2D, "Series 2", "Category 5");
        defaultCategoryDataset.addValue(1.0D, "Series 3", "Category 1");
        defaultCategoryDataset.addValue(3D, "Series 3", "Category 2");
        defaultCategoryDataset.addValue(5D, "Series 3", "Category 3");
        defaultCategoryDataset.addValue(2D, "Series 3", "Category 4");
        defaultCategoryDataset.addValue(0.0D, "Series 3", "Category 5");
        return defaultCategoryDataset;
    }

    @Override
    protected JFreeChart createChart(Dataset dataset) {
        CategoryDataset categoryDataset = (CategoryDataset) dataset;
        JFreeChart jFreeChart = ChartFactory.createLineChart("Line Chart Demo 8", "Category",
                "Count", categoryDataset, PlotOrientation.VERTICAL,
                true, true, false);

        CategoryPlot categoryPlot = (CategoryPlot) jFreeChart.getPlot();
        SymbolAxis symbolAxis = new SymbolAxis("Group", new String[] {
                "A", "B", "C", "D", "E", "F"
        });
        categoryPlot.setRangeAxis(symbolAxis);
        ChartUtils.applyCurrentTheme(jFreeChart);

        NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
        numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot.getRenderer();
        lineAndShapeRenderer.setSeriesShapesVisible(0, true);
        lineAndShapeRenderer.setSeriesShapesVisible(1, false);
        lineAndShapeRenderer.setSeriesShapesVisible(2, true);
        lineAndShapeRenderer.setSeriesLinesVisible(2, false);
        lineAndShapeRenderer.setSeriesShape(2, ShapeUtils.createDiamond(4F));
        lineAndShapeRenderer.setDrawOutlines(true);
        lineAndShapeRenderer.setUseFillPaint(true);
        lineAndShapeRenderer.setDefaultFillPaint(Color.white);
        return jFreeChart;
    }

    public static void main(String[] args) {
        LineChartDemo8 demo1 = new LineChartDemo8("LineChartDemo7");
        ChartUtil.showChartDemo(demo1);
    }
}
